from sanic import Sanic
import aiomysql
import aioredis
from sanic import response
import asyncio
import json
from . import app_user_update
from db_connect.conn_mysql import ConnMysql
from db_connect.conn_redis import ConnRedis
from db_connect.conn_slave_mysql import ConnMysqlSlave

@app_user_update.route("/order_status/<ID:int>",methods=['POST'])
async def user(request,ID):

    if not request.method == 'POST':
    
        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    user_info = request.form
    
    status = user_info.get("status")

    if not status:
    
        return response.json({
                'status': -1,
                'errorCode': 3,
                'errorMsg':f"缺少参数status{status}",
                'resultBody':None
            })
    async with ConnMysql().pool.acquire() as c:
        async with c.cursor(aiomysql.DictCursor) as cur:

            sql = 'UPDATE orders SET status = %s WHERE id = %s'

            await cur.execute(sql,(status,ID))
            await c.commit()

            return response.json({
                'status': 1,
                'errorCode': '',
                'errorMsg':"",
                'resultBody':"FINISH"
            })